<?php
namespace app\controller;

use app\BaseController;
use FceskyUpload\contract\FileInfo;
use FceskyUpload\UploadManager;
use think\Request;

class Index extends BaseController
{
    public function index()
    {
        return $this->fetch('index');
    }

    public function receive(Request $request){
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            exit; // finish preflight CORS requests here
        }
        try{
            $config = config('fceskyupload');
            $uploadManager = new UploadManager($config);
            $fileInfo = new FileInfo();
            $fileInfo->identifier = $request->param('hash');
            $fileInfo->filename = $request->param('name');
            $fileInfo->chunkNumber = $request->param("chunk");
            $fileInfo->chunkNumber = intval($fileInfo->chunkNumber ?? 0);
            $fileInfo->totalChunks = intval($request->param("chunks") ?? 1);
            $fileInfo->totalChunks = $fileInfo->totalChunks ?? 1;
            $fileInfo->totalSize = $request->param('size');
            $fileInfo->ext = $request->param('ext');
            // $fileInfo->ext = str_replace('.','',$fileInfo->ext);
            $ret = $uploadManager->uploadFile($fileInfo);
            $ret = $uploadManager->checkFile($fileInfo);

        }catch(\Exception $e){
            return json($e->getMessage());
        }
        
        // dump($ret);exit;
        // return json(['code'=>1,'msg'=>'success']);
    }

    





}
